import { ApiProperty } from '@nestjs/swagger'
import { Column, Entity, JoinTable, ManyToMany, Relation } from 'typeorm'

import { CompleteEntity } from '~/common/entity/common.entity'
import { CommonStatus } from '~/enums/common.enum'
import { UserEntity } from '~/modules/system/user/user.entity'

@Entity('system_post')
export class PostEntity extends CompleteEntity {
  @ApiProperty({ description: '岗位编码' })
  @Column({ type: 'varchar', length: 64, unique: true, comment: '岗位编码' })
  code: string

  @ApiProperty({ description: '岗位名称' })
  @Column({ type: 'varchar', length: 50, comment: '岗位名称' })
  name: string

  @ApiProperty({ description: '显示顺序' })
  @Column({ type: 'int', comment: '显示顺序' })
  sort: number

  @ApiProperty({ description: '状态（0正常 1停用）', enum: CommonStatus })
  @Column({ type: 'tinyint', default: CommonStatus.NORMAL, comment: '状态（0正常 1停用）' })
  status: CommonStatus

  @ApiProperty({ description: '备注' })
  @Column({ type: 'varchar', length: 500, nullable: true, comment: '备注' })
  remark: string

  @ManyToMany(() => UserEntity, user => user.posts)
  @JoinTable({
    name: 'system_user_post',
    joinColumn: { name: 'post_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'user_id', referencedColumnName: 'id' },
  })
  users: Relation<UserEntity[]>
}
